<?php
/**
 * Created by jinqiang on 2024/7/2 16:32.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Services\Oms;


use App\Http\Models\Mysql\SsyJyProductListTable;
use App\Http\Repository\OmsRepository;

class JuyiService
{
    public function search($param)
    {
        $field = ["type_no","product_name","product_code","a_parts","commodity_classification","tag_price","selling_price","cost_price","img","num","use_num","brand_code"];
        $conn = (new SsyJyProductListTable())->select($field);
        if($param['type_no'] ?? ""){
            $conn = $conn->where(function ($query) use ($param){
                $query->where('product_code', 'like', $param['type_no']."%")
                    ->orWhere('type_no', $param['type_no'])
                    ->orWhere('product_name', 'like', "%".$param['type_no']."%");
            });
        }
        if($param['sku_code'] ?? ""){
            $conn = $conn->where('product_code', 'like', $param['sku_code']."%");
        }
        if($param['product_name'] ?? ""){
            $conn = $conn->where('product_name', 'like', "%".$param['product_name']."%");
        }
        if($param['a_parts'] ?? ""){
            $conn = $conn->where('a_parts', 'like', "%".$param['a_parts']."%");
        }
        $user = auth()->guard("api")->user();
        $subjects = array_column(config("scm.subject"), null, 'is_subject');
        $isSubject = $user->is_subject;
        $minus_3 = -3;
        if($param['is_subject'] ?? ""){
            $minus_3 = 0;//非借赠内购，不需要-3
            $isSubject = $param['is_subject'];
        }
        $virtualWarehouseCode = $subjects[$isSubject]['virtual_warehouse_code'];
        $discount = $subjects[$isSubject]['discount'];
        $brandCode = $subjects[$isSubject]['brand_code_jy'];
        $specialSku = ["ZPDYTZZ025"];//特殊规格，同时需要在成人和童装下搜索
        if(in_array($isSubject,['kz','mz']) && in_array($param['sku_code'] ?? "", $specialSku)){
            $conn = $conn->whereIn('brand_code', ['SSY', 'SSYE']);
        }else{
            $conn = $conn->where('brand_code', $brandCode);
        }
        $res = $conn->orderBy('product_code', 'asc')->paginate((Int)$param['limit'])->toArray();
        $skuCodes = array_column($res['data'], 'product_code');
        //查询巨益可销售、库存、在途数
        $omsSellableQuantity = [];
        $omsQuantity = [];
        $omsTransitQuantity = [];
        if(!empty($skuCodes)){
            $omsRes = OmsRepository::searchNum($virtualWarehouseCode, $skuCodes);
            $omsList = $omsRes['stockInfoList'] ?? [];
            $omsSellableQuantity =  array_column($omsList, "sellableQuantity", "skuCode");
            $omsQuantity = array_column($omsList, "quantity", "skuCode");
            $omsTransitQuantity = array_column($omsList, "transitQuantity", "skuCode");
        }
        foreach($res['data'] as &$item){
            //销售价
            $item['selling_price'] = (Double)$item['selling_price'];
            //折扣
            $item['discount'] = $discount;
            //可销数
            $item['use_num'] = ($omsSellableQuantity[$item['product_code']] ?? 0) + $minus_3;
            //库存数
            $item['num'] = ($omsQuantity[$item['product_code']] ?? 0) + $minus_3;
            //在途数
            $item['qty'] = ($omsTransitQuantity[$item['product_code']] ?? 0) + $minus_3;
        }
        unset($item);
        return [
            'data' => $res['data'],
            'total' => $res['total']
        ];
    }
}
